package com.nbsaas.boot.nbtask.task;

import com.nbsaas.boot.nbtask.domain.OrderMessage;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.stereotype.Component;

@Component
public class OrderProducer {

    private final JmsTemplate jmsTemplate;

    public OrderProducer(JmsTemplate jmsTemplate) {
        this.jmsTemplate = jmsTemplate;
    }

    public void sendOrder(OrderMessage orderMessage) {
        jmsTemplate.convertAndSend("order-queue", orderMessage);
        //System.out.println("Order sent: " + orderMessage.getOrderId());
    }


    public void sendOrder(OrderMessage orderMessage, long delayMillis) {
        jmsTemplate.setDeliveryPersistent(true); // 确保开启持久投递
        jmsTemplate.convertAndSend("order-queue", orderMessage, message -> {
            message.setLongProperty("_AMQ_SCHED_DELAY", delayMillis);
            message.setLongProperty("AMQ_SCHEDULED_DELAY", delayMillis); // 设置延迟 5 秒
            return message;
        });
    }
}
